package com.example.batteryhelper;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

class DialogHelper {

    public static Dialog DeleteDialogShow(Context context, String title, String content, View.OnClickListener confirmListener) {

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_delete, null);
        final Dialog dialog = new AlertDialog.Builder(context, R.style.MyDialog).setView(view).create();
        TextView text_title = view.findViewById(R.id.txt_title_dialog_delete);
        TextView text_content = view.findViewById(R.id.txt_content_dialog_delete);
        TextView quxiao = view.findViewById(R.id.dialog_btn_delete_no);
        TextView queding = view.findViewById(R.id.dialog_btn_delete);
        text_title.setText(title);
        text_content.setText(content);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        queding.setOnClickListener(confirmListener);

        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
        return dialog;
    }

    public static Dialog ChooseDialogShow(Context context, int selected, String[] btn_txt,ChooseDialogListener listener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_select, null);
        final Dialog dialog = new AlertDialog.Builder(context, R.style.MyDialog).setView(view).create();
        int[] radioBtn_id = {R.id.radio_btn_0, R.id.radio_btn_1, R.id.radio_btn_2, R.id.radio_btn_3
                , R.id.radio_btn_4, R.id.radio_btn_5, R.id.radio_btn_6, R.id.radio_btn_7};
//        RadioGroup radioGroup = view.findViewById(R.id.radioGroup_dialog);
        TextView textView = view.findViewById(R.id.tv_ok);
        RadioButton radioButton = view.findViewById(radioBtn_id[selected % 8]);
        radioButton.setChecked(true);
        for (int i = 0; i <Math.min(radioBtn_id.length,btn_txt.length) ; i++) {
            RadioButton radioButton1=view.findViewById(radioBtn_id[i]);
            radioButton1.setText(btn_txt[i]);
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < radioBtn_id.length; i++) {
                    RadioButton radioButton1=view.findViewById(radioBtn_id[i]);
                    if (radioButton1.isChecked()){
                        listener.onClick(i);
                    }

                }

                dialog.dismiss();
            }
        });

        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.8 * screenWidth);
        window.setAttributes(params);
       return dialog;
    }

    public interface ChooseDialogListener{
         void onClick(int selected);
    }
}


